package com.carkeeper.system.service.impl;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.dao.DepartmentDao;
import com.carkeeper.system.pojo.Department;
import com.carkeeper.system.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周志雄
 * @version 1.0
 * @date 2020/11/23 20:05
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 部门更新
     *
     * @param department
     * @return
     */
    @Override
    public Result update(Department department) {
        int i = departmentDao.updateByPrimaryKeySelective(department);

        if (i == 0) {
            return new Result(false, StatusCode.ERROR, "更新失败");
        }

        return new Result(true, StatusCode.OK, "更新成功");
    }

    /**
     * 条件查询
     *
     * @param map
     * @return
     */
    @Override
    public Map queryByCondition(Map map) {
        int pageNum;
        int pageSize;

        if (StringUtils.isBlank((String) map.get("page"))) {
            pageNum = 1;
        } else {
            pageNum = Integer.parseInt((String) map.get("page"));
        }

        if (StringUtils.isBlank((String) map.get("pagesize"))) {
            pageSize = 10;
        } else {
            pageSize = Integer.parseInt((String) map.get("pagesize"));
        }

        PageHelper.startPage(pageNum, pageSize);

        Example example = this.createExample(map);

        List<Department> item = departmentDao.selectByExample(example);

        PageInfo info = new PageInfo(item);

        //总条数
        long counts = info.getTotal();
        //总页数
        int pages = info.getPages();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("counts", counts);
        resultMap.put("pages", pages);
        resultMap.put("page", pageNum);
        resultMap.put("item", item);

        return resultMap;
    }

    @Override
    public Result add(Department department) {
        department.setCreateTime(new Date());
        int i = departmentDao.insertSelective(department);

        if (i == 0) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }

        return new Result(true, StatusCode.OK, "添加成功");
    }

    @Override
    public Result delete(Long id) {
        Department department = departmentDao.selectByPrimaryKey(id);
        department.setIsdelete(StatusCode.IS_DELETE_YES);

        int i = departmentDao.updateByPrimaryKeySelective(department);

        if (i == 0) {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }

        return new Result(true, StatusCode.OK, "删除成功");
    }

    @Override
    public List<Department> findAll() {
        return departmentDao.selectAll();
    }

    private Example createExample(Map<String, Object> searchMap) {

        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();

        //删除状态为未删除
        criteria.andEqualTo("isdelete", StatusCode.IS_DELETE_NO);

        if (searchMap != null) {
            // 部门名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 部门编码
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andLike("code", "%" + searchMap.get("code") + "%");
            }
        }

        return example;
    }
}
